# -*- coding: utf-8 -*-
import scrapy
import request


from http import cookiejar


cookie =cookiejar.CookieJar()
cookie_handler = request.HTTPCookieProcessor(cookie)
http_handler = request.HTTPHandler()
https_handler = request.HTTPSHandler()
opener = request.build_opener(http_handler, https_handler, cookie_handler)

class RenSpider(scrapy.Spider):
    name = 'Ren'
    allowed_domains = ['renren.com']

    def start_requests(self):
        urls = [
            'http://www.renren.com/',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def Login(self):
        """
        负责初次登录
        需要输入用户名和密码，用来获取登录cookie凭证
        :return:
        """
        # url为登录表单提交地址
        url = "http://www.renren.com/PLogin.do"

        user = input("用户：")
        pwd = input("密码：")

        data = {
            "email": user,
            "password": pwd
        }
        # 数据进行编码
        data = self.parse.scrapy(data)
        req = scrapy.Request(url, data=data.encode())
        rsp = opener.open(req)
